﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace LightSO.Library.Common
{
    public class CookiesHelper
    {
        /// <summary>
        /// Cookie主域名
        /// </summary>
        readonly string CookieDomain = HttpContext.Current.Request.Url.Host;

        EncryptionHelper encryption;

        public CookiesHelper()
        {
            encryption = new EncryptionHelper();
        }

        /// <summary>
        /// 设置HttpCookie并加密
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="minute"></param>
        public void SetCookieByDESEncrypt(string key, string value, int minute = 0)
        {
            value = encryption.DESEncrypt(value);
            SetCookie(key, value, minute);
        }

        /// <summary>
        /// 设置HttpCookie
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="value">值</param>
        /// <param name="minute">有效期的分钟数</param>
        public void SetCookie(string key, string value, int minute)
        {
            var cookie = new HttpCookie(key, value);
            cookie.Domain = CookieDomain;
            cookie.Expires = DateTime.Now.AddMinutes(minute);
            HttpContext.Current.Response.SetCookie(cookie);
        }
        /// <summary>
        /// 设置HttpCookie,默认有效期为1天
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="value">值</param>
        public void SetCookie(string key, string value)
        {
            SetCookie(key, value, 24 * 60);
        }
        /// <summary>
        /// 取Cookie的值
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns>空则返回string.Empty</returns>
        public string GetCookie(string key)
        {
            var cookie = HttpContext.Current.Request.Cookies.Get(key);
            if (null == cookie)
                return string.Empty;

            return cookie.Value;
        }

        /// <summary>
        /// 取Cookie的值并解密
        /// </summary>
        /// <param name="key"></param>
        public string GetCookieByDESDecrypt(string key)
        {
            var resultString = GetCookie(key);
            if (string.IsNullOrWhiteSpace(resultString))
                return string.Empty;

            return encryption.DESDecrypt(resultString);
        }

        /// <summary>
        /// 清除Cookie
        /// </summary>
        public void RemoveAll()
        {
            for (int i = 0; i < HttpContext.Current.Request.Cookies.Count; i++)
            {
                RemoveCookies(HttpContext.Current.Request.Cookies[i].Name);
            }
        }

        /// <summary>
        /// 删除一个具体的Cookie
        /// </summary>
        /// <param name="key"></param>
        public void RemoveCookies(string key)
        {
            var cookie = HttpContext.Current.Request.Cookies.Get(key);
            if (null != cookie)
            {
                cookie.Domain = CookieDomain;
                cookie.Value = "";
                cookie.Expires = DateTime.Now.AddYears(-1);
                HttpContext.Current.Response.Cookies.Set(cookie);
            }
        }
    }
}
